package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import cqrtplm.service.RTReviseObjectByEcnService;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;

@CustomListener(
        category = "润通",
        description = "将ECN变更对象修订升版并添加到变更后对象"
)
public class RTReviseObjectByEcnListener implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(RTReviseObjectByEcnListener.class);
    private static final long serialVersionUID = -3947300634487283870L;

    public void notify(DelegateExecution delegateExecution) {
        try {
            log.info("开始执行监听 ==> 将ECN变更对象修订升版RTReviseObjectByEcnListener");
            String workFlowContainerId = delegateExecution.getProcessInstanceBusinessKey();
            if (StringUtils.isBlank(workFlowContainerId)) {
                workFlowContainerId = delegateExecution.getVariable("workFlowContainerId").toString();
            }
            RTReviseObjectByEcnService rtReviseObjectByEcnService = (RTReviseObjectByEcnService) SpringUtil.getBean(
                    RTReviseObjectByEcnService.class);
            rtReviseObjectByEcnService.reviseEcaObjectByEcn(new BigInteger(workFlowContainerId));
            log.info("正常执行监听结束 <== 将ECN变更对象修订升版RTReviseObjectByEcnListener");
        } catch (Exception e) {
            log.error("监听RTReviseObjectByEcnListener异常：", e);
            throw new RuntimeException(e.getMessage(),e);
        }
    }
}
